package swing;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.ibatis.session.SqlSession;

import baseDao.UserDao;
import baseDao.VpnDao;
import entity.User;
import entity.Vpn;
import util.GetSqlSession;
import util.MD5Util;

public class UpdatePassword extends JDialog {

	//public static UpdatePassword updatePassword = new UpdatePassword(null);

	public UpdatePassword(JFrame aqa) {
		super(aqa, true);
		this.setTitle("修改vpn");
		//this.setDefaultCloseOperation(3);
		this.setSize(320, 220);
		this.setLocationRelativeTo(null);
		this.setResizable(false);

		Container c = this.getContentPane();
		c.setBackground(Color.white);
		c.setLayout(null);

		/*
		 * c.addMouseMotionListener(new MouseMotionListener() {
		 * 
		 * @Override public void mouseMoved(MouseEvent e) { int x=e.getX(); int
		 * y=e.getY(); System.out.println(x+"  "+y); }
		 * 
		 * @Override public void mouseDragged(MouseEvent e) { } });
		 */

		// 背景图片
		JLabel j = new JLabel(new ImageIcon("image/loginBg.jpg"));
		j.setBounds(0, 0, 420, 70);
		//c.add(j);

		// 头像
		// JLabel j2 = new JLabel(new ImageIcon("image/touxiang.png"));
		// j2.setBounds(35, 165, 75, 75);
		// c.add(j2);
		JLabel j1 = new JLabel("账号：");
		j1.setBounds(20, 35, 60, 30);
		c.add(j1);
		JLabel j2 = new JLabel("密码：");
		j2.setBounds(20, 65, 60, 30);
		c.add(j2);
		JLabel jl3 = new JLabel("新密码：");
		jl3.setBounds(60, 98, 60, 30);
		//c.add(jl3);
		JLabel newjl3 = new JLabel("确认密码：");
		newjl3.setBounds(20, 128, 70, 30);
		//c.add(newjl3);

		// 账号框
		final JTextField jt0 = new JTextField();
		jt0.setBounds(115, 35, 180, 30);
		c.add(jt0);

		// 旧密码
		final JPasswordField oldjw = new JPasswordField();
		oldjw.setBounds(115, 65, 180, 30);
		c.add(oldjw);

		// 新密码
		final JPasswordField newjw = new JPasswordField();
		newjw.setBounds(115, 145, 180, 30);
		//c.add(newjw);

		// 确认密码
		final JPasswordField enterjw = new JPasswordField();
		enterjw.setBounds(115, 175, 180, 30);
		//c.add(enterjw);

		// 提示6-10
		JLabel hint = new JLabel("6-10位");
		hint.setBounds(300, 175, 180, 30);
		//c.add(hint);

		// 修改
		JButton j3 = new JButton();
		ImageIcon icon = new ImageIcon("image/upw.png");
		j3.setIcon(icon);
		j3.setBounds(105, 120, 80, 25);

		j3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				// 获取用户输入的账号
				String username = jt0.getText();

				// 获取用户输入的旧密码
				char[] c = oldjw.getPassword();
				String oldPassword = new String(c);

				// 新密码
				char[] c2 = newjw.getPassword();
				String newPassword = new String(c2);

				// 确认密码
				char[] c3 = enterjw.getPassword();
				String enterjw = new String(c3);

				SqlSession session = GetSqlSession.getSession();
				// 获取user接口:
				VpnDao vpDao = session.getMapper(VpnDao.class);
				Vpn v=new Vpn();
				v.setAcNu(username);
				v.setPw(oldPassword);
					// 修改
					int update = vpDao.update(v);
					System.out.println("修改：：：：" + update);

					if (update == 1) {
						
						UpdatePassword.this.setVisible(false);
						
						// 进入登录
						//new Login().setVisible(true);
						GetSqlSession.closeSession(session);
						JOptionPane.showMessageDialog(null, "成功！");
					} else {
						JOptionPane.showMessageDialog(null, "失败...");
					}
			}
		});
		c.add(j3);
	}

}
